package com.immunology.servlets;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.immunology.dao.MedicineCardDao;
import com.immunology.database.transformers.MedicineCardTransformer;
import com.immunology.model.AttributeContent;
import com.immunology.model.MappingPages;
import com.immunology.model.MedicineCard;
import com.immunology.model.Patient;
import com.immunology.model.Survey;
import com.immunology.model.User;
import com.immunology.service.PatientService;
import com.immunology.service.SurveyService;
import com.immunology.service.UserService;

public class ShowPatientServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public ShowPatientServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int patientId = 0;
		@SuppressWarnings("unchecked")
		Enumeration<String> enumeration = request.getParameterNames();
		while (enumeration.hasMoreElements()) {
			patientId = Integer.parseInt(enumeration.nextElement());
		}
		PatientService patientService = new PatientService();
		MedicineCardDao medicineCardDao = new MedicineCardDao(
				new MedicineCardTransformer());
		MedicineCard medicineCard = null;
		Patient patient = null;
		try {
			patient = patientService.searchPatientById(patientId);
			medicineCard = medicineCardDao
					.searchMedicineCardByPatientId(patientId);
		} catch (SQLException e) {
			System.out.println("Can't get data from patient table!");
			e.printStackTrace();
		}

		SurveyService surveyService = SurveyService.getInstance();
		List<Survey> surveyList = surveyService
				.getSurveyListByMedicineCardId(medicineCard.getId());

		Map<Survey, String> surveyDoctorMap = new LinkedHashMap<Survey, String>();
		UserService userService = new UserService();

		for (Survey survey : surveyList) {
			int userId = survey.getDoctorId();
			User user = userService.getUserById(userId);
			String doctorName = user.getLastName() + " " + user.getFirstName()
					+ " " + user.getMiddleName();
			surveyDoctorMap.put(survey, doctorName);
		}

		getServletContext().setAttribute(AttributeContent.PATIENT, patient);
		getServletContext().setAttribute(AttributeContent.MEDICINE_CARD,
				medicineCard);
		getServletContext().setAttribute(AttributeContent.SURVEY_DOCTOR_MAP,
				surveyDoctorMap);
		getServletContext().getRequestDispatcher(MappingPages.SHOW_PATIENT_INFO_JSP)
				.forward(request, response);

	}

}
